Phone Number Validation For (SMS) mTAN

Goals

Client-side scripting

Example client-side scripting


Enter a phone number. Also try copy&paste, e. g. +41 (0) 31-555 00 11

Code
×

<script>
function v(phone) {
phone=phone.replace(/\(+.*?\)+/g,' ');
phone=phone.replace(/[^0-9\ /+]/g,' ');
phone=phone.replace(/^\s+/g, '');
if(phone.substr(0,2)=='00')phone='+'+phone.substr(2,phone.length);
if(phone.substr(0,1)>0)phone='+'+phone;
if(phone.substr(1,1)>0&&phone.substr(0,1)!='+'&&phone.substr(1,2)!='')phone='+41'+phone.substr(1,phone.length);
phone=phone.replace(/ +/g,' ');
phone=phone.replace(/^\s+/g,'');
phone=phone.replace(/[^0-9\ +]/g,'');
if(phone.replace(/\s/g,'').indexOf('+410')===0)phone='+41'+phone.replace(/\s/g,'').substr(4,phone.replace(/\s/g,'').length);
if(phone.split('+').length>2)phone='+'+phone.split('+')[1];
if(phone.substr(0,1)=='+'&&phone.substr(1,1)===0)phone='';
return phone;
}
</script>

Further, the form field HTML tag for the phone number must contain the following:
onchange="if(v(this.value)!=this.value)this.value=v(this.value)" onkeyup="this.onchange()" onfocus="this.onchange()" onblur="this.onchange()" onmouseover="this.onchange()" onmouseout="this.onchange()"

Additional server-side scripting (recommended)

Download

Content Management System u5CMS

Service

English⇄German Translation Service

Flowers

blumen-bern-maarsen.ch